package cn.zmall.manager.service.impl;

import cn.zmall.common.jedis.JedisClient;
import cn.zmall.manager.dto.OrderInfo;
import cn.zmall.manager.mapper.*;
import cn.zmall.manager.pojo.*;
import cn.zmall.manager.service.OrderManagerService;
import com.alibaba.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author cxy
 * @Date 2020/12/2 10:22
 * @Version 1.0
 */
@Service(interfaceClass = OrderManagerService.class)

public class OrderManagerServiceImpl implements OrderManagerService {

    private final static Logger log = LoggerFactory.getLogger(OrderManagerService.class);

    @Autowired
    private TbOrderMapper tbOrderMapper;//订单
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;//商品
    @Autowired
    private TbOrderShippingMapper tbOrderShippingMapper;//购物记录

    //删除订单
    @Override
    public int delOrder(String orderId) {
        if (tbOrderMapper.deleteByPrimaryKey(orderId) != 1) {
            return -2;
        }
        TbOrderItemExample example = new TbOrderItemExample();
        TbOrderItemExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<TbOrderItem> list = tbOrderItemMapper.selectByExample(example);
        for (TbOrderItem tbOrderItem : list) {
            if (tbOrderItemMapper.deleteByPrimaryKey(tbOrderItem.getId()) != 1) {
                return -2;
            }
        }
        if (tbOrderShippingMapper.deleteByPrimaryKey(orderId) != 1) {
            return -2;
        }
        return 1;
    }

    //取消订单
    @Override
    public int cancelOrder(String orderId) {
        TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(orderId);
        if (tbOrder == null){
            return -1;
        }
        tbOrder.setCloseTime(new Date());
        tbOrder.setUpdateTime(new Date());
        tbOrder.setStatus(5);
        if (tbOrderMapper.updateByPrimaryKey(tbOrder) != 1) {
            return -2;
        }
        return 1;
    }

    //订单发货
    @Override
    public int deliverOrder(String orderId, String shippingName, String shippingCode, BigDecimal postFee) {
        TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(orderId);
        if (tbOrder == null){
            return -1;
        }
        tbOrder.setShippingName(shippingName);
        tbOrder.setShippingCode(shippingCode);
        tbOrder.setPostFee(postFee);
        tbOrder.setConsignTime(new Date());
        tbOrder.setUpdateTime(new Date());

        tbOrder.setStatus(3);
        if (tbOrderMapper.updateByPrimaryKey(tbOrder) != 1) {
            return -2;
        }
        return 1;
    }

    //退货
    @Override
    public int salesReturn(String orderId) {
        TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(orderId);
        if (tbOrder == null){
            return -1;
        }
        tbOrder.setStatus(-1);
        tbOrder.setUpdateTime(new Date());
        if (tbOrderMapper.updateByPrimaryKey(tbOrder) != 1) {
            return -2;
        }
        return 1;
    }

    //备注
    @Override
    public int remarkOrder(String orderId, String message) {
        TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(orderId);
        if (tbOrder == null){
            return -1;
        }
        tbOrder.setBuyerMessage(message);
        tbOrder.setUpdateTime(new Date());
        if (tbOrderMapper.updateByPrimaryKey(tbOrder) != 1) {
            return -2;
        }
        return 1;
    }

    //获取订单详情
    @Override
    public OrderInfo getOrderDetail(String orderId) {
        OrderInfo orderInfo = new OrderInfo();

        TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(orderId);
        if (tbOrder == null){
            return null;
        }

        TbOrderItemExample tbOrderItemExample = new TbOrderItemExample();
        TbOrderItemExample.Criteria criteria = tbOrderItemExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<TbOrderItem> tbOrderItems = tbOrderItemMapper.selectByExample(tbOrderItemExample);

        TbOrderShipping tbOrderShipping = tbOrderShippingMapper.selectByPrimaryKey(orderId);

        orderInfo.setTbOrder(tbOrder);
        orderInfo.setTbOrderItem(tbOrderItems);
        orderInfo.setTbOrderShipping(tbOrderShipping);
        return orderInfo;
    }

    @Override
    public Long countOrder() {
        TbOrderExample tbOrderExample = new TbOrderExample();
        Long count = tbOrderMapper.countByExample(tbOrderExample);
        if (count == null) {
            return Long.valueOf(-1);
        }
        return count;
    }

    @Override
    public List<TbOrder> getOrderList() {
        TbOrderExample tbOrderExample = new TbOrderExample();
        TbOrderExample.Criteria criteria = tbOrderExample.createCriteria();
        criteria.andOrderIdIsNotNull();
        List<TbOrder> tbOrders = tbOrderMapper.selectByExample(tbOrderExample);
        return tbOrders;
    }
}
